package cate.game.activity.sevendaytrial;

import cate.common.table.activity.sevendaytrial.progress.SevenDayTrialProgressRow;
import cate.common.table.activity.sevendaytrial.task.SevenDayTrialTaskRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.sevendaytrial.msg.SevenDayTrialInfoResp;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.task.TaskGroup;
import easy.java.dev.note.NoteField;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 七日试炼数据
 *
 * @author ShenSL
 * @since 2023/10/18 14:20
 */
public class SevenDayTrialActivity extends RoleActivitySingle {
    @NoteField("每日任务完成数量")
    public Map<Integer, Integer> dailyTaskDone;
    @NoteField("每日任务完成奖励领取状态")
    public Map<Integer, Boolean> dailyTargetGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (dailyTaskDone == null) {
            dailyTaskDone = new HashMap<>();
        }
        if (dailyTargetGot == null) {
            dailyTargetGot = new HashMap<>();
        }
    }

    @Override
    public void excelCheck() {
        // 任务检测
        reloadTasks();
        // 加载天数数据
        Set<Integer> days = role.getGame().table.sevenDayTrial.task.getList().stream()
                .filter(row -> row.configId == configTid())
                .map(row -> row.day)
                .collect(Collectors.toSet());
        days.stream().filter(day -> !dailyTaskDone.containsKey(day))
                .forEach(day -> dailyTaskDone.put(day, 0));
        days.stream().filter(day -> !dailyTargetGot.containsKey(day))
                .forEach(day -> dailyTargetGot.put(day, false));
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        if (!enable()) {
            return;
        }
        reloadTasks();
    }

    @Override
    public void onRoleEnd() {
        role.getTask().removeGroup(funcId());
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    /**
     * 发送消息
     */
    private void noticeUpdate() {
        role.sendNow(new SevenDayTrialInfoResp(this));
    }

    /**
     * 重载任务
     */
    private void reloadTasks() {
        int curDay = getCurrentDay();
        Set<Integer> rows = role.getGame().table.sevenDayTrial.task.getList().stream()
                .filter(row -> row.configId == configTid() && row.day <= curDay)
                .map(row -> row.taskId)
                .collect(Collectors.toSet());
        TaskGroup taskGroup = role.getTask().getGroup(funcId());
        List<Integer> removeIds = taskGroup.taskIdSet.stream()
                .filter(id -> !rows.contains(id))
                .collect(Collectors.toList());
        taskGroup.removeItemList(removeIds);
        taskGroup.addItemListNoRepeat(new ArrayList<>(rows));
    }

    /**
     * 任务完成增加进度
     *
     * @param taskId 完成的任务ID
     */
    public void onTaskDone(int taskId) {
        SevenDayTrialTaskRow targetRow = role.getGame().table.sevenDayTrial.task.getList().stream()
                .filter(row -> row.configId == configTid() && row.taskId == taskId)
                .findAny()
                .orElse(null);
        if (targetRow == null) {
            return;
        }
        if (dailyTaskDone.containsKey(targetRow.day)) {
            dailyTaskDone.put(targetRow.day, dailyTaskDone.get(targetRow.day) + 1);
        } else {
            dailyTaskDone.put(targetRow.day, 1);
        }
        noticeUpdate();
    }

    public GameResult<Void> onGetTargetReward(int day) {
        GameResult<Void> r = new GameResult<>();
        int curDay = getCurrentDay();
        if (day > curDay) {
            return r.fail("未解锁");
        }
        SevenDayTrialProgressRow progressRow = role.getGame().table.sevenDayTrial.progress.getList().stream()
                .filter(row -> row.configId == configTid() && row.day == day)
                .findAny()
                .orElse(null);
        if (progressRow == null) {
            return r.fail("找不到配置");
        }
        if (dailyTargetGot.containsKey(day) && dailyTargetGot.get(day)) {
            return r.fail("不可重复领取");
        }
        if (!dailyTaskDone.containsKey(day) || dailyTaskDone.get(day) < progressRow.taskDoneNum) {
            return r.fail("未达到领奖条件");
        }
        dailyTargetGot.put(day, true);
        MixRes reward = new MixRes(progressRow.rewardStr);
        new MixResAdder().setRes(reward)
                .setOperation(GDOperation.ACT_SEVEN_DAY_TRIAL_PROGRESS)
                .setOperationSub(progressRow.id)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        noticeUpdate();
        return r.success();
    }
}
